package org.advanced.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author John.zhang
 * @version Id: NioWrite, v 1.0 2020/12/15 15:39 ZhiYong Exp $
 */
public class NioWrite {

    public static void main(String[] args) {
        try (FileOutputStream fileOutputStream = new FileOutputStream("C:/data/io/io_cp.txt")) {
            FileChannel fileChannel = fileOutputStream.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            //往缓冲区(byteBuffer)写数据
            byteBuffer.put("Hello,I'm John".getBytes());
            //根据上面和下面的读写不同模式这里需要进行转换，读模式转化为写模式
            byteBuffer.flip();
            //读取缓冲区(byteBuffer)的数据
            fileChannel.write(byteBuffer);
            byteBuffer.clear();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
